/*
PearReview - The peer review assistant.
Copyright (C) 2009  Dimitrij Pankratz, Anton Musichin
http://www.pearreview.com, info@pearreview.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

package com.pearreview.util.pdf;

import java.io.IOException;
import java.util.ArrayList;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.pearreview.application.Messages;

/**
 * This class is for printing images from findings to the PDF document.
 * The functionality is useful for the protocol in scribe mode and 
 * report of the reviewer mode.
 *
 */
public class PdfImageWriter {

	/**
	 * Write all added images for printing them.
	 * 
	 * @param fImages
	 * @param doc
	 * @param writer
	 * @throws DocumentException
	 * @throws IOException
	 */
	public void writeImages(ArrayList<PdfImage> fImages, Document doc,
			PdfWriter writer) throws DocumentException, IOException {
		
		PChunk cHeading = new PChunk(Messages.getString(
				"PearReview.Scribe.findings.images"));
		Font font = cHeading.getFont();
		font.setColor(new BaseColor(0x000000));
		font.setStyle(Font.NORMAL);
		font.setSize(20);
		
		Paragraph pHeading = new PParagraph();
		pHeading.setSpacingAfter(40);
		pHeading.add(cHeading);
		
		doc.add(pHeading);
			
		
		//write all images
		for (PdfImage image : fImages) {
			
			Chunk fileName = new PChunk(image.getFileName())
					.setLocalDestination(image.getGoTo());
			Chunk attachPin = new PChunk(" - ").setAnnotation(
					image.getAttachment());
			
			Paragraph content = new PParagraph();
			content.add(attachPin);
			content.add(fileName);
			content.add(new PChunk(",  "));
			content.add(new PChunk(Messages.getString(
					"PearReview.protocol.see_finding") + image.getId() )
					.setLocalGoto("ID"+ image.getId()));
			content.add(new PChunk("   "));
			//content.add(attachPin);
			
			content.setSpacingAfter(10f);
			
			Image img = Image.getInstance(image.getImage());
			if (img.getHeight()>480 || img.getWidth()>480)
				img.scaleToFit(480, 480);
			
			writer.setStrictImageSequence(true);
			
			//doc.add(content);
			//doc.add(img);
			
			// wrap the paragraph in a PdfPTable no
			PdfPTable aTable = new PdfPTable(1);
			aTable.setWidthPercentage(100f);
			PdfPCell cell = new PdfPCell(content);
			cell.setBorder(0);
			cell.setBorderWidth(0f);
			cell.setPaddingBottom(10f);
			aTable.addCell(cell);
			
			cell = new PdfPCell(img);
			cell.setBorder(0);
			cell.setBorderWidth(0f);
			aTable.addCell(cell);
			
			//the most important line of code
			//table is maybe the only element in iText, which works 
			//correctly with this.
			aTable.setKeepTogether(true);

			doc.add(aTable);
			
			Paragraph pLast = new PParagraph();
			pLast.setSpacingAfter(30f);

			doc.add(pLast);
		}
	}
}
